package com.study.services;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.study.model.jdao.IDepotgoodsShelfDAO;
import com.study.model.mdao.DeportFormMapper;
import com.study.model.mdao.DepotgoodsshelfMapper;
import com.study.model.mdao.GoodDAO;
import com.study.model.mdao.OrderFormDetailMapper;
import com.study.model.mdao.OrderFormMapper;
import com.study.model.pojo.Depotgoodsshelf;
import com.study.model.vo.Cart;

@Service
@Transactional(rollbackOn = Exception.class)
public class DepotgoodsshelfService {
	@Autowired
	IDepotgoodsShelfDAO dao;
	@Autowired
	DepotgoodsshelfMapper mapper;
	@Autowired
	OrderFormDetailMapper rDetailMapper;
	@Autowired
	GoodDAO goodDAO;
	@Autowired
	DeportFormMapper deportFormMapper;

	@Autowired
	OrderFormMapper orderFormMapper;

	/**
	 * 根据编号查询仓库
	 * 
	 * @param dgsnum
	 * @return
	 */
	public Depotgoodsshelf selectById(int dgsnum) {
		return dao.findByDgsnum(dgsnum);
	}

	/**
	 * 查询所有
	 * 
	 * @return
	 */
	public List<JSONObject> selectAll() {
		return mapper.selectAll();
	}

	/**
	 * 查询所有
	 * 
	 * @param oid
	 * @return
	 */
	public JSONObject selectDeportGoods(Integer oid) {
		JSONObject jsonob = new JSONObject();
		// 放入所有仓库信息
		jsonob.put("deportshelf", selectAll());
		// 查询订单明细里的商品以及商品数量
		List<JSONObject> findByOdidDetail = rDetailMapper.findByOdidDetail(oid);
		List<JSONObject> list = new ArrayList<>();// 用来存储商品的数量、对应仓库数量
		for (JSONObject jsonObject : findByOdidDetail) {
			JSONObject jsc = new JSONObject();
			// 查询商品信息
			JSONObject findByGoodsId = goodDAO.findByGoodsId(jsonObject.getInteger("goo_id"));
			jsc.put("gid", jsonObject.getInteger("goo_id"));
			jsc.put("gname", findByGoodsId.getString("goo_name"));
			jsc.put("gqueaty", jsonObject.getInteger("od_nonum"));
			// 查询每个仓库对应此商品的数量
			List<JSONObject> selectGoods = mapper.selectGoods(jsonObject.getInteger("goo_id"));
			putGoodsDeportNum(selectGoods, jsc);// 将对应仓库的数量也放入进来

			System.out.println("出库的信息封装" + jsc);
			list.add(jsc);
		}
		jsonob.put("goodItem", list);
		jsonob.put("oid", oid);
		return jsonob;
	}

	/**
	 * 将每个仓库对应此商品的数量放入传入的json中
	 * 
	 * @param selectGoods
	 * @param jsc
	 */
	public void putGoodsDeportNum(List<JSONObject> selectGoods, JSONObject jsc) {
		String nn = "";
		Integer a = 0, b = 0, c = 0;
		for (JSONObject jsonObject : selectGoods) {
			nn = jsonObject.getString("dgsname");
			if ("最好的仓库".equals(nn)) {
				a = jsonObject.getInteger("quatity");
			} else if ("全国仓库".equals(nn)) {
				b = jsonObject.getInteger("quatity");
			} else if ("北京仓库".equals(nn)) {
				c = jsonObject.getInteger("quatity");
			}
		}
		jsc.put("adeport", a);
		jsc.put("bdeport", b);
		jsc.put("cdeport", c);
	}

	/**
	 * 通过订单id，出库对应仓库id的商品
	 * 
	 * @param oid 订单id
	 * @param deport 仓库id
	 * @param uid 申请出库单的销售员id
	 */
	public void addOutDeportText(Integer oid, Integer deport, Integer uid) {
		// 查询订单明细里的商品以及商品数量
		List<JSONObject> findByOdidDetail = rDetailMapper.findByOdidDetail(oid);
		for (JSONObject jsonObject : findByOdidDetail) {
			//deportFormMapper.deletTrubyDetailid(jsonObject.getInteger("o_id"));
			//查询批次出库商品直到出库的商品达到需求
			outDeportEnd(oid, deport, uid, jsonObject);
		}
	}
	
	/**
	 * 查询批次出库商品直到出库的商品达到需求
	 * @param oid
	 * @param deport
	 * @param uid
	 * @param jsonObject
	 */
	public void outDeportEnd(Integer oid, Integer deport, Integer uid,JSONObject jsonObject) {
		//查询对应订单一些信息
		JSONObject findByIds = orderFormMapper.findByIds(oid);
		//查询出满足条件的库存批次
		List<JSONObject> findDeportToCnShelf = deportFormMapper.findDeportToCnShelf(jsonObject.getInteger("goo_id"), deport);
		int sums=0;
		sums=jsonObject.getInteger("od_nonum");//获取订单要的数量，作为最大数量
		System.out.println(sums);
		for (JSONObject pjs : findDeportToCnShelf) {
			sums-=pjs.getInteger("dsurplu");
			if (sums>0) {
				//如果需求数大于库存数量，此时直接出库此库存所有商品
				System.out.println(sums+"大于库存数量"+pjs.getInteger("dsurplu"));
				deportFormMapper.addOutDeports(uid, findByIds.getString("otitle"), pjs.getInteger("dsurplu"),  jsonObject.getInteger("o_id"), pjs.getInteger("dnum"));
			}else if (sums<0){
				//如果需求数量减去库存小的话代表库存数大于需求数了，此时这个批次出库的数量等于计算后的需求数量
				System.out.println(sums+"小于库存数量"+pjs.getInteger("dsurplu")+" 此时的："+(pjs.getInteger("dsurplu")+sums));
				deportFormMapper.addOutDeports(uid, findByIds.getString("otitle"), pjs.getInteger("dsurplu")+sums,  jsonObject.getInteger("o_id") , pjs.getInteger("dnum"));
				break;
			}else if (sums==0){
				//如果需求数量减去库存等于0的话代表库存数等于需求数了，此时这个批次出库的数量等于计算后的需求数量
				System.out.println(sums+"大于库存数量"+pjs.getInteger("dsurplu"));
				deportFormMapper.addOutDeports(uid, findByIds.getString("otitle"), pjs.getInteger("dsurplu"),  jsonObject.getInteger("o_id") , pjs.getInteger("dnum"));
				break;
			}
		}
	}

}
